

#import "AblumViewController.h"
#import "AblumView.h"
#import "MainViewController.h"
#import "WXNetworkService.h"
#import "ImagesModel.h"

@interface AblumViewController ()

-(void)loadContentView;

-(void)loadFooterTitle;

-(void)requestData;

-(void)refreshUI;

@end

@implementation AblumViewController

-(void)loadView
{
    [super loadView];
    self.title = @"电影图片";
    
    [self loadContentView];
    
    [self loadFooterTitle];
    
}
-(void)loadContentView
{
    _SCrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kDeviceWidth+15, kDeviceHeight)];
    _SCrollView.delegate=self;
    _SCrollView.backgroundColor=[UIColor clearColor];
    _SCrollView.pagingEnabled=YES;
    _SCrollView.showsHorizontalScrollIndicator=NO;
    _SCrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_SCrollView];
}
-(void)loadFooterTitle
{
    _footerLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, kDeviceHeight-44, kDeviceWidth, 44)];
    _footerLabel.backgroundColor=[UIColor blackColor];
    _footerLabel.font=[UIFont systemFontOfSize:16];
    _footerLabel.textColor=[UIColor whiteColor];
    _footerLabel.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:_footerLabel];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self requestData];
	// Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationItem.hidesBackButton= YES;
    self.tabBarController.tabBar.hidden = YES;
}

-(void)requestData
{
    NSArray *data=[WXNetworkService newsImageData];
    _imageArray=[[NSMutableArray alloc]initWithCapacity:data.count];
    for (NSDictionary *content in data) {
        ImagesModel *imagemodel=[[ImagesModel alloc]initWithContent:content];
        [_imageArray addObject:imagemodel];
        
    }
    _SCrollView.contentSize=CGSizeMake(_imageArray.count*(kDeviceWidth+15), 0);
    [self refreshUI];
    
}

-(void)refreshUI
{
    ImagesModel *imagemodel=_imageArray[0];
    _footerLabel.text=imagemodel.title;
    int x=0;
    for (int index=0; index<_imageArray.count; index++)
    {
        imagemodel=_imageArray[index];
        AblumView *ablum=[[AblumView alloc]initWithFrame:CGRectMake(0+x, 0, kDeviceWidth, kDeviceHeight)];
        ablum.tag=index+1000;
        ablum.delegate=self;
        ablum.url=imagemodel.url;
        
        if (index<2)
        {
            [ablum downloadImage];
        }
        
        [_SCrollView addSubview:ablum];
        x+=(kDeviceWidth+15);
    }
}
-(void)downloadData:(NSInteger)i
{
    ImagesModel *imagemodel=_imageArray[i];
    AblumView *ablum=(AblumView *)[_SCrollView viewWithTag:i+1000];
    ablum.url=imagemodel.url;
    [ablum downloadImage];
    
}

#pragma ScrollView代理
static int lastIndex=0;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int index=_SCrollView.contentOffset.x/(kDeviceWidth+15);
    ImagesModel *model=_imageArray[index];
    if (index<0||index>=_imageArray.count)
    {
        return;
    }
    
    _footerLabel.text=model.title;
    AblumView *ablum=(AblumView *)[_SCrollView viewWithTag:lastIndex+1000];
    
    if (ablum.singleScroll.zoomScale>=1&&index!=lastIndex)
    {
        ablum.singleScroll.zoomScale=1;
    }

    if (index==0)
    {
    }
    else if(index==_imageArray.count-1)
    {
        [self downloadData:_imageArray.count-1];
    }
    else
    {
        // index  2
        // 1 < = 3
        for (int i=index-1; i<=index+1; i++)
        {
            
            [self downloadData:i];
        }
    }
    lastIndex=index;
}
-(void)hiddenORshow:(AblumView *)ablum
{
    [UIView animateWithDuration:0.5 animations:^{
        if (_footerLabel.alpha==1 && self.navigationController.navigationBar.hidden == NO)
        {
            _footerLabel.alpha=0;
            self.navigationController.navigationBar.hidden = YES;
        }
        else
        {
            _footerLabel.alpha=1;
            self.navigationController.navigationBarHidden = NO;
        }
    }];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
